package com.yyks.cnbooking.dubbo.service.redis;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author dangy
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RedisServiceImpl implements RedisService {

    private final StringRedisTemplate stringRedisTemplate;

    private final RedisTemplate redisTemplate;

    @Override
    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void setAndExpire(String key, String value, long expire, TimeUnit timeUnit) {
        stringRedisTemplate.opsForValue().set(key, value);
        stringRedisTemplate.expire(key, expire, timeUnit);
    }

    @Override
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    @Override
    public boolean expire(String key, long expire, TimeUnit timeUnit) {
        return stringRedisTemplate.expire(key, expire, timeUnit);
    }

    @Override
    public void remove(String key) {
        stringRedisTemplate.delete(key);
    }

    @Override
    public Long increment(String key, long delta) {
        return stringRedisTemplate.opsForValue().increment(key,delta);
    }

    @Override
    public void saveHashList(List<Map> mapList, String key, String hashKey, long expire, TimeUnit timeUnit) {

        redisTemplate.opsForHash().put(key,hashKey,mapList);
        redisTemplate.expire(key,expire,timeUnit);
    }

    @Override
    public void saveHashString(Map map, String key, String hashKey, long expire, TimeUnit timeUnit) {

        redisTemplate.opsForHash().put(key,hashKey,map);
        redisTemplate.expire(key,expire,timeUnit);
    }

    @Override
    public List<Map> getList(String key) {

        //List<String> objectList = Lists.newArrayList();
        List<Map> values = redisTemplate.opsForHash().values(key);
        return values;
    }

    @Override
    public Map getHashString(String key, String hashKey) {

        return (Map) redisTemplate.opsForHash().get(key,hashKey);
    }

    @Override
    public void deleteHashString(String key, String hashKey) {

        redisTemplate.opsForHash().delete(key, hashKey);
    }

    @Override
    public List<Map> getHashList(String key, String hashKey) {

        return (List<Map>) redisTemplate.opsForHash().get(key,hashKey);
    }
}
